<template>
  <!-- 当点击 取消按钮 时，关闭弹窗 -->
  <div>
    <el-drawer
      :title="title"
      :visible.sync="visible"
      :append-to-body="true"
      @close="clearDialog()"
      :wrapperClosable="false"
      size="60%"
      v-loading="loding"
      :close-on-click-modal="false"
    >
      <div class="pack">
        <ele-form
          v-model="dialogfrom"
          v-bind="formConfig"
          ref="form"
        ></ele-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="clearDialog()">{{
            $t("common.cancel")
          }}</el-button>
          <!-- <el-button type="primary" v-if="barcodeInfo.isCreateBarcode != 1" @click="creatBoxCode()">{{
        $t('mesTrackingCardPrint.create') }}
      </el-button> -->
          <!-- <el-button
            type="primary"
            v-if="barcodeInfo.isCreateTrackcode != 1"
            @click="creatBoxCode()"
            >生码
          </el-button> -->
          <el-button type="primary" @click="creatBoxCode(true)"
            >{{ $i18n.t("common.print") }}
          </el-button>
          <!-- <template v-else>
            <el-button type="danger" @click="clearBoxcode()">{{
              $t("mesTrackingCardPrint.clear")
            }}</el-button>
            <el-button type="primary" @click="creatBoxCode(true)"
              >打印
            </el-button>
          </template> -->
        </div>
      </div>
    </el-drawer>
    <templateSelect
      ref="templateSelect"
      v-model="dialogfrom.barcodePrintTempCode"
      :searchData="{
        goodsCode: this.dialogfrom.prodCode,
        businessTypeCode: 'PRINT_MES_PC_TRACKING_CARD',
      }"
      @select="
        (data) => {
          dialogfrom.barcodePrintTemp = data.name;
        }
      "
    ></templateSelect>
  </div>
</template>

<script>
import {
  printTemplateInfo,
  mesProdBoxcode,
  getBoxCount,
  clearBoxcode,
  mesTrackingCard as creatBoxCode, //生码打印
} from "@/api/trackingcardprint/mesTrackingCardPrint";
import { setPrintData } from "@/utils/judge.js";
import templateSelect from "@/components/templateSelect/index.vue";
let arrBarcodePrintingList2 = [];

export default {
  name: "barcodePrintingAddModel",
  components: { templateSelect },
  watch: {
    visible(val, oldVal) {
      if (!val) {
        this.$nextTick(() => {
          this.$refs.form.resetForm();
        });
        this.barcodeInfo = {};
      }
    },
  },
  props: {
    title: {
      type: String,
      default: "",
    },
  },

  data() {
    let that = this;
    return {
      loding: false,
      formConfig: {
        span: 24,
        isShowBackBtn: false,
        isShowSubmitBtn: false,
        labelPosition: "right",
        formDesc: {
          orderCode: {
            type: "input",
            label: $i18n.t("mesTrackingCardPrint.workOrderCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          barcodePrintTemp: {
            type: "input",
            label: $i18n.t("mesTrackingCardPrint.barcodePrintTemp"),
            layout: 12,
            disabled: () => {
              // return this.barcodeInfo.isCreateBarcode == 1 ? true : false
              // return this.barcodeInfo.isCreateTrackcode == 1 ? true : false;
            },
            attrs: {
              readonly: true,
            },
            // disabled={
            //           that.barcodeInfo.isCreateTrackcode == 1 ? true : false
            //         }
            required: true,
            slots: {
              append(h) {
                return (
                  <el-button
                    icon="el-icon-search"
                    onClick={() => {
                      that.$refs.templateSelect.show();
                    }}
                  ></el-button>
                );
              },
            },
          },
          prodCode: {
            type: "input",
            label: $i18n.t("mesTrackingCardPrint.materialCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          prodName: {
            type: "input",
            label: $i18n.t("mesTrackingCardPrint.materialName"),
            layout: 12,
            required: true,
            disabled: true,
          },
          quantityPrinted: {
            type: "number",
            label: $i18n.t("mesTrackingCardPrint.printedQuantity"),
            layout: 12,
            required: true,
            disabled: true,
          },
          // quantityNotPrinted: {
          //   type: "number",
          //   label: $i18n.t("mesTrackingCardPrint.notPrintedQuantity"),
          //   layout: 8,
          //   required: true,
          //   disabled: true,
          // },
        },
      },
      barcodeInfo: {},
      dialogfrom: {},
      visible: false,
      loading: false,
      list: [],
      print: {
        id: "printArea1",
      },
      time: 1,
    };
  },
  methods: {
    //初始化
    init(params) {
      console.log("params", params);
      this.dialogfrom = params;
      this.barcodeInfo = params;
      this.visible = true;
      let { printedNum, notPrintedNum } = this.dialogfrom;
      this.dialogfrom.quantityPrinted = printedNum;
      this.dialogfrom.quantityNotPrinted = notPrintedNum;
      this.dialogfrom.barcodePrintTemp = "";
      this.dialogfrom.barcodePrintTempCode = "";
    },

    async getBoxCount(callback) {
      let res = await getBoxCount({
        orderCode: this.dialogfrom.orderCode,
      });
      this.dialogfrom = res.data.result;

      callback ? callback(res.data.result) : "";
    },
    // 打印
    async printBoxcode() {
      this.$refs.form.validate().then(async (valid) => {
        this.getBoxCount((data) => {
          let { barcodePrintTemp, barcodePrintTempCode } = data;
          // / 包装打印预备数据存缓存
          setPrintData(
            {
              code: barcodePrintTempCode,
              name: barcodePrintTemp,
              status: this.dialogfrom.quantityPrinted > 0 ? 1 : 0,
            },
            [data]
          );
          this.clearDialog();
          this.$router.push("/splitPrint");
        });
      });
    },
    async clearBoxcode() {
      try {
        let { value } = await this.$prompt(
          $i18n.t("mesTrackingCardPrint.inputClearReason"),
          $i18n.t("common.tip"),
          {
            confirmButtonText: $i18n.t("common.ok"),
            cancelButtonText: $i18n.t("common.cancel"),
            inputValidator: (value) => {
              if (!value) {
                return $i18n.t("mesTrackingCardPrint.inputClearReason");
              }
            },
          }
        );
        if (!value) {
          this.$message.warning(
            $i18n.t("mesTrackingCardPrint.inputClearReason")
          );
          return;
        }
        this.loding = true;
        let res = await clearBoxcode({
          clearReason: value,
          orderCode: this.dialogfrom.orderCode,
        });

        let deflt = this.dialogfrom.count;
        this.dialogfrom.count = 999;
        this.$nextTick(() => {
          this.dialogfrom.count = deflt;
          this.getBoxCount();
          // this.$set(this.barcodeInfo, 'isCreateBarcode', 0)
          this.$set(this.barcodeInfo, "isCreateTrackcode", 0);
        });
      } finally {
        this.loding = false;
      }
    },

    //修改容量
    countChange(e) {
      let quantity = Number(this.dialogfrom.quantity);
      let printQuantity = Math.ceil(quantity / e);
      this.dialogfrom.printQuantity = printQuantity;
    },
    //点击生码按钮
    creatBoxCode(bool) {
      this.$refs.form.validate().then((valid) => {
        let {
          orderCode,
          count,
          barcodePrintTemp,
          boxCodeRule,
          barcodePrintTempCode,
          printQuantity,
        } = this.dialogfrom;
        let data = {
          orderCode: orderCode, //'生产工单号',
          barcodePrintTemp: barcodePrintTemp, //'打印条码模板名称',
          // boxCodeRule: boxCodeRule,
          barcodePrintTempCode: barcodePrintTempCode, //'打印条码模板名称',
        };
        this.loding = true;
        creatBoxCode(data)
          .then((res) => {
            if (res.data.success) {
              //  this.$refs.print.$refs.printing.$el.click();
              // this.$message.success(
              //   $i18n.t("mesTrackingCardPrint.createSuccess")
              // );
              // let deflt = this.dialogfrom.count;
              // this.dialogfrom.count = 999;
              // this.dialogfrom.id = res.data.result;
              this.$nextTick(() => {
                // this.dialogfrom.count = deflt;

                // this.$set(this.barcodeInfo, 'isCreateBarcode', 1)
                this.$set(this.barcodeInfo, "isCreateTrackcode", 1);
                if (bool) {
                  // this.getBoxCount((data) => {
                  let { barcodePrintTemp, barcodePrintTempCode } = data;
                  // / 包装打印预备数据存缓存
                  setPrintData(
                    {
                      code: barcodePrintTempCode,
                      name: barcodePrintTemp,
                      status: 0,
                    },
                    [res.data.result]
                  );
                  this.clearDialog();
                  this.$router.push("/splitPrint");
                  // });
                } else {
                  this.getBoxCount();
                }
              });
            }
            this.loding = false;
          })
          .catch((error) => {
            this.loding = false;
          });
      });
    },

    clearDialog() {
      this.visible = false;
      this.$emit("refreshList");
    },
  },
};
</script>
<style lang="scss" scoped>
.pack {
  padding: 20px;
  padding-top: 0px;
}
.dialog-footer {
  padding-top: 20px;
  display: flex;
  justify-content: end;
}
</style>
